<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 01:05:11
 * @LastEditTime: 2023-10-16 12:56:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Observers;

use Carbon\Carbon;

class BaseObserver
    {

    /**
     * Handle the model "created" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function created($model)
        {
        $use_traits = class_uses($model);
        //trait中是否有删除函数
        $use_traits && array_filter($use_traits, function ($trait) use ($model)
            {
            $traitName = (substr($trait, strrpos($trait, "\\") + 1));
            if (method_exists($model, $traitName . 'Created')) {
                $model->{$traitName . 'Created'}($model);
                }
            });
        if (method_exists($model, '_created'))
            $model->_created($model);
        //
        }

    /**
     * Handle the model "created" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function creating($model)
        {

        //自动生成新增的lot

        if ($model->_auto_resetlot_name && isNotEmpty($model->lot_name)) {

            if ($model->lot_id == 0 || $model->lot_id == null)
                $model->lot_id = $model->_resetLot();
            }

        if (property_exists($model, "timestamps") && $model->timestamps) {
            $user = auth()->user();
            if ($user)
                $model['updator_id'] = $model['creator_id'] = $user->employee_id;
            $model['created_at'] = date("Y-m-d H:i:s");
            $model['updated_at'] = date("Y-m-d H:i:s");
            }
        //
        //自動編碼
        if (property_exists($model, "ACSign")) {
            $model->generateCode();
            }
        if (method_exists($model, '_creating'))
            return $model->_creating($model);
        }

    public function deleting($model)
        {

        $use_traits = class_uses($model);
        //trait中是否有删除函数
        $use_traits && array_filter($use_traits, function ($trait) use ($model)
            {
            $traitName = (substr($trait, strrpos($trait, "\\") + 1));
            if (method_exists($model, $traitName . 'Deleting')) {
                $model->{$traitName . 'Deleting'}($model);
                }
            });

        if (method_exists($model, '_deleting'))
            return $model->_deleting($model);
        }
    /**
     * Handle the model "updated" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function updated($model)
        {

        $use_traits = class_uses($model);
        //trait中是否有更新函数
        $use_traits && array_filter($use_traits, function ($trait) use ($model)
            {
            $traitName = (substr($trait, strrpos($trait, "\\") + 1));
            if (method_exists($model, $traitName . 'Updated')) {
                $model->{$traitName . 'Updated'}($model);
                }
            });
        if (method_exists($model, '_updated'))
            return $model->_updated($model);
        }

    public function updating($model)
        {

        if (property_exists($model, "timestamps") && $model->timestamps) {
            $user = auth()->user();
            if ($user)
                $model['updator_id'] = $user->employee_id;
            }
        if (method_exists($model, '_updating'))
            return $model->_updating($model);
        }


    /**
     * Handle the model "deleted" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function deleted($model)
        {
        //
        if (method_exists($model, '_deleted'))
            return $model->_deleted($model);
        }

    /**
     * Handle the model "restored" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function restored($model)
        {
        if (method_exists($model, '_restored'))
            return $model->_restored($model);
        //
        }

    /**
     * Handle the model "force deleted" event.
     *
     * @param  \App\Models\BaseModel  $model
     * @return void
     */
    public function forceDeleted($model)
        {
        if (method_exists($model, '_forceDeleted'))
            return $model->_forceDeleted($model);
        //
        }
    }
